//
//  GoldenVPNServiceListVC.m
//  QLadder
//
//  Created by 苹果王 on 2019/8/19.
//  Copyright © 2019 qding. All rights reserved.
//

#import "GoldenVPNServiceListVC.h"
#import "GoldenVPNServiceListCell.h"
#import "GoldenVPNPaymentVC.h"

static NSString * serviceListVCToPaymentVCSegue = @"serviceListVCToPaymentVCSegue";

@interface GoldenVPNServiceListVC ()

@property (nonatomic, strong) NSArray * dataArray;
@property (nonatomic, strong) NSString * orderNo;

@end

@implementation GoldenVPNServiceListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    [kNetworkUtil postWithModuleURLStr:productData parameters:nil success:^(id response) {
        if ([[response objectForKey:@"code"] integerValue] == 200) {
            self.dataArray = response[@"netTcVO"];
            [self.tableView reloadData];
        }
    } failure:nil withHud:YES];
    
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoldenVPNServiceListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoldenVPNServiceListCell"];
    NSDictionary * dic = self.dataArray[indexPath.row];
    cell.dic = dic;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [HUDUtil showWithTitle:@"确定购买此套餐吗？" viewController:self handler:^(UIAlertAction *action) {
        NSDictionary * dic = self.dataArray[indexPath.row];
        NSString * productId = [NSString stringWithFormat:@"%@", dic[@"productId"]];
        NSString * userName = [kUserDefaults objectForKey:kUserAccount];
        if (userName && productId) {
            [kNetworkUtil postWithModuleURLStr:createOrder parameters:@{@"loginName" : userName, @"productId" : productId} success:^(id response) {
                if ([[response objectForKey:@"code"] integerValue] == 200) {
                    NSString * orderNo = response[@"orderNo"];
                    if (orderNo.length) {
                        self.orderNo = orderNo;
                        [self performSegueWithIdentifier:serviceListVCToPaymentVCSegue sender:self];
                    }
                }
            } failure:nil withHud:YES];
        }
    }];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:serviceListVCToPaymentVCSegue]) {
        GoldenVPNPaymentVC * payVC = segue.destinationViewController;
        payVC.orderNo = self.orderNo;
    }
}


@end
